﻿Imports System.Linq
Imports System.Web.DomainServices
Imports System.Web
Imports System.Data.Linq

Partial Public Class Site
    Inherits System.Web.UI.MasterPage

    Private _page As New Data.Page

    Public Property PageData() As Data.Page
        Get
            Return Me._page
        End Get
        Set(ByVal value As Data.Page)
            Me._page = value
        End Set
    End Property

    'Todo : add notes...

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim title = HttpContext.Current.Items("pageName")

        Dim ctx = New Data.SiteDataClassesDataContext()

        ctx.DeferredLoadingEnabled = False

        Dim options As DataLoadOptions = New DataLoadOptions()

        options.LoadWith(Of Data.Page)(Function(p As Data.Page) p.aspnet_User)

        ctx.LoadOptions = options

        Dim pages As IQueryable(Of SilverlightSEO.Data.Page)

        If title IsNot Nothing Then


            pages = From p In ctx.Pages Where p.Title = title.ToString().Replace("_", " ")



        Else

            pages = From p In ctx.Pages Order By p.SortIndex Ascending


        End If

        Me.PageData = pages.FirstOrDefault()


        If Me.PageData IsNot Nothing Then

            Me.MetaDescription.Content = Me.PageData.Description
            Me.MetaKeywords.Content = Me.PageData.Keywords



        Else

            If title Is Nothing Then
                title = "Default Page"
            End If

            Me.PageData = New Data.Page() With {.Title = title.ToString(), .aspnet_User = New Data.aspnet_User()}


        End If

    End Sub

End Class